let WxParse = require('../../wxParse/wxParse.js');
import grace from "../../utils/grace.js"
grace.page({
      data: {
            menu_id: 0,
            info: {},
            goods: [],
            descHtml: {}
      },
      onLoad: function(options) {
            if (options) {
                  var menu_id = options[0][0].menu_id;
                  this.$data.menu_id = menu_id;
                  this.$http.get('api/goods_menu/detail', {
                        menu_id: menu_id
                  }).then((res) => {
                        this.$data.info = res;
                        this.$data.goods = res.goods_list;
                        try {
                              WxParse.wxParse('descHtml', 'html', res.content, this, 0);
                        } catch (e) {
                              console.error(e);
                        }
                  })
            };
      },
      // 增加数量
      addCount: function(e) {
            const index = e.currentTarget.dataset.index;
            let goods = this.data.goods;
            let num = goods[index].goods_number;
            num = parseInt(num) + 1;
            goods[index].goods_number = num;
            this.$data.goods = goods;
      },
      // 减少数量
      minusCount: function(e) {
            const index = e.currentTarget.dataset.index;
            let goods = this.data.goods;
            let num = goods[index].goods_number;
            if (parseInt(num) > 1) {
                  num = parseInt(num) - 1;
                  goods[index].goods_number = num;
                  this.$data.goods = goods;
            }
      },
      onShareAppMessage: function() {
            let share_url = 'pages/menu_recommend/menu_recommend?menu_id=' + this.$data.menu_id;
            return {
                  title: '菜谱推荐：' + this.$data.info.title,
                  path: share_url,
                  success: function(res) {

                  },
                  fail: function(res) {
                        // 转发失败
                  }
            }
      },
      openDetail: function(e) {
            let goods_id = e.currentTarget.dataset.goods;
            wx.navigateTo({
                  url: '../product_details/product_details?goods_id=' + goods_id,
            })
      },
      addToCart: function(e) {
            if (!wx.getStorageSync('api_token')) {
                  this.toast('您还没登录，请先登陆后在操作');
                  setTimeout(function() {
                        grace.bus.$emit("usre_logout_success");
                  }, 1000);
                  return;
            }
            let goods = this.$data.goods;
            let goods_id = new Array();
            let goods_num = new Array();
            for (var i = 0, len = goods.length; i < len; i++) {
                  goods_id.push(goods[i]['goods_id']);
                  goods_num.push(goods[i]['goods_number']);
            }
            let goods_vals = goods_id.join(',');
            let goods_num_vals = goods_num.join(',');
            this.$http.post('api/cart/add_goods', {
                  goods_vals: goods_vals,
                  goods_number: goods_num_vals
            }).then((res) => {
                  this.toast(res.message);
                  this.$bus.$emit("update_cart_event");
            });
      }
})